﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DL;
using System.Data.SqlClient;
using System.Data;
using System.Data.EntityClient;
using System.Configuration;

namespace BL
{
    public class ServiceBase
    {
        public DateTime GetDateGobal()
        {
         
            return this.Context.ExecuteStoreQuery<DateTime>("SELECT GETDATE()").First();

        }

        private LTOEntities context;


        public LTOEntities Context
        {

            get
            {
                if (this.context == null)
                {
                    this.context = new LTOEntities();

                }
                return this.context;

            }
        }



        public void ClearContext()
        {
            this.context = new LTOEntities();
           
        }


        public ServiceBase()
        {
           // this.context = new LTOEntities(BuildEntityConnectionString());
        }
        internal ServiceBase(LTOEntities context)
        {
            this.context = context;


        }
        private string BuildEntityConnectionString()
        {
            //string connectionString = new GobalRef.DBService().CallConnection("LTOEntities");
            string providerName = "System.Data.SqlClient";
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
            entityBuilder.Provider = providerName;
           // entityBuilder.ProviderConnectionString = connectionString; //@"Data Source=.;Initial Catalog=LTO;Integrated Security=True;MultipleActiveResultSets=True;";

            // Set the Metadata location.

            entityBuilder.Metadata = @"res://*/GobalDB.csdl|
                            res://*/GobalDB.ssdl|
                            res://*/GobalDB.msl";


            return entityBuilder.ToString();
        }
      
        public SqlParameter CreateParamater(string Name, object Value)
        {
            SqlParameter paramValue = new SqlParameter(string.Format("@{0}", Name), Value);
            paramValue.Value = Value;
            return paramValue;
        }
        public List<T> ExecuteStoreQuery<T>(string sql)
        {
            return this.Context.ExecuteStoreQuery<T>(sql.ToString()).ToList();
        }
        public List<T> ExecuteStoreQuery<T>(string sql, List<SqlParameter> param)
        {
            return this.Context.ExecuteStoreQuery<T>(sql.ToString(), param.ToArray()).ToList();
        }
        public DataSet GetDateSet(string Query)
        {

            string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["VexpressConnectionString"].ConnectionString;

            SqlDataAdapter a = new SqlDataAdapter(Query, ConnectionString);
            DataSet ds = new DataSet();
            a.Fill(ds, "resulttable");
            return ds;
        }

    }
}
